﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Solitaire {
    class Table {
        const int columns = 7;
        int[] column = new int[columns];
        List<KlondikeSlot> klondike = new List<KlondikeSlot>();
        List<Slot> end = new List<Slot>();
        List<Slot> all = new List<Slot>();
        Slot pile = new Slot();
        Slot discard = new Slot();
        KlondikeSlot mouse = new KlondikeSlot();
        Deck deck;
        GameWindow window;

        public Table(GameWindow window) {
            deck = new Deck();
            this.window = window;

            for (int i = 0; i < columns; i++) {
                klondike.Add(new KlondikeSlot());
            }

            pile = new Slot();

            for(int i = 0; i < 4; i++) {
                end.Add(new Slot());
            }

            all.Union(klondike);


            resized();
            deal();
        }

        public void deal() {
            Card tmpCard;
            for (int i = 0; i < klondike.Count; i++) {
                for (int k = 0; k <= i; k++) {
                    tmpCard = deck.drawCard();
                    if (k == i)
                        tmpCard.flip();
                    klondike.ElementAt<KlondikeSlot>(i).addCard(tmpCard);
                }
            }
        }

        public void LoadContent(ContentManager content) {
            deck.LoadContent(content);
            foreach (KlondikeSlot ks in klondike)
                ks.LoadContent(content);
            foreach (Slot s in end)
                s.LoadContent(content);
            pile.LoadContent(content);
            discard.LoadContent(content);
            mouse.LoadContent(content);
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch) {
            foreach(KlondikeSlot ks in klondike) {
                ks.Draw(gameTime, spriteBatch);
            }
            pile.Draw(gameTime, spriteBatch);
            discard.Draw(gameTime, spriteBatch);
            foreach (Slot s in end) {
                s.Draw(gameTime, spriteBatch);
            }
            mouse.Draw(gameTime, spriteBatch);
        }

        public void resized() {
            int space = window.ClientBounds.Width / columns;
            for (int i = 0; i < column.Length; i++)
                column[i] = space * i + (space - Card.width) / 2;

            KlondikeSlot ks;

            for (int i = 0; i < klondike.Count; i++) {
                ks = klondike.ElementAt(i);
                ks.Position = new Vector2(column[i], 107);
                ks.Rect = new Rectangle((int)ks.Position.X, (int)ks.Position.Y, ks.Rect.Width, window.ClientBounds.Height - (int)ks.Position.Y);
            }

            pile.Position = new Vector2(column[0], 6);
            discard.Position = new Vector2(column[1], 6);

            Slot s;
            for (int i = 0; i < end.Count; i++) {
                s = end.ElementAt(i);
                s.Position = new Vector2(column[(columns - end.Count + i)], 6);
            }
        }

        public void MouseClicked(InputControl ic) {
            Card tmpCard = null;
            if (mouse.Count() == 0) {
                Slot sl = ic.slotUnderMouse(all);
                if ((tmpCard = ic.cardUnderMouse(sl.toList(), mouse)) != null)
                    mouse.addCards(sl.remCards(tmpCard));

            }
        }
    }
}
